package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 * http://bbs.ifeng.com/ 凤凰论坛
 * @author allen
 */
public class IfengBbsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://bbs.ifeng.com/viewthread.php[?]photoId=1&tid=[0-9]*",
				"http://bbs.ifeng.com/viewthread.php.*page=1.*tid=.*",
				"http://bbs.ifeng.com/viewthread.php.*tid=.*page=1.*",
				"http://bbs.ifeng.com/viewthread.php.*page%3D1.*tid=.*",
				"http://bbs.ifeng.com/viewthread.php.*tid=.*page%3D1.*",
				"http://bbs.ifeng.com/viewthread.php\\?tid=[0-9]*"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		Integer clickNum=0;
		Integer commentNum=0;

		Document doc = Jsoup.parse(htmltxt);

			title =doc.select("title").text();
			if(title==null || title.trim().length()==0){
			}
			if(title.indexOf("-")!=-1){
				title=title.substring(0,title.indexOf("-"));
			}

			content = HtmlCleaner.getContentHtml(url, doc.select("div#content form div.left_post div.content01 div.article"));
			if(content==null || content.trim().length()==0){
				content = HtmlCleaner.getContentHtml(url, doc.select("span#main_intro"));
			}

			String tempdate=doc.select("div#content form div.left_post div.content01 div.artfooter ul.right").text();
			if(tempdate==null || tempdate.trim().length()==0){
				tempdate=doc.select("div#moretab div.top div.date").text();
			}
			date = DateUtils.matchDate(tempdate);


			bbs.setTitle(StringUtils.trimSpace(title));
			bbs.setContent(content);
			bbs.setDate(date);

			String clickComentstr=doc.select("div#content div.content01 div.artfooter").text();
			if(clickComentstr!=null && clickComentstr.trim().length()!=0){
				if(clickComentstr.indexOf("点击：")!=-1){
					String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("点击：")+"点击：".length());
					if(clickNumstr.indexOf(" ")!=-1){
						clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf(" "));
						try {
							clickNum=Integer.parseInt(clickNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
				if(clickComentstr.indexOf("回复：")!=-1){
					String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("回复：")+"回复：".length());
					if(commentNumstr.indexOf(" ")!=-1){
						commentNumstr=commentNumstr.substring(0,commentNumstr.indexOf(" "));
					try {
						commentNum=Integer.parseInt(commentNumstr);
					} catch (Exception e) {
						// TODO: handle exception
					}
					}
				}
			}else{
				 clickComentstr=doc.select("html body div.col form div.colbox div#moretab.ledBox div.top div.date").text();
				 if(clickComentstr!=null && clickComentstr.trim().length()!=0){
					 if(clickComentstr.indexOf("点击：")!=-1){
							String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("点击：")+"点击：".length());
							if(clickNumstr.indexOf(" ")!=-1){
								clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf(" "));
								try {
									clickNum=Integer.parseInt(clickNumstr);
								} catch (Exception e) {
									// TODO: handle exception
								}
							}
						}
						if(clickComentstr.indexOf("回复：")!=-1){
							String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("回复：")+"回复：".length());


							try {
								commentNum=Integer.parseInt(commentNumstr);
							} catch (Exception e) {
								// TODO: handle exception
							}

						}
				 }
			}
			if(bbs.getDate()==null){
				bbs.setDate(DateUtils.matchDate(doc.select("div.article div.survey h2 span.tt1").text()));
			}
			bbs.setClickNum(clickNum);
			bbs.setCommentNum(commentNum);

			String author=doc.select("div#content form div.left_post div.content01 div.member table tbody tr td p span.fb").text();
			if(author!=null){
				author=author.replace("作者：", "");
			}
			if(author.trim().length()==0){
				author=doc.select("div.col form div.colbox div#moretab div.top div.date a").text();
			}
			bbs.setAuthor(author);
			/**
			 * 解析用于更新的地址
			 */
			bbs.setUpdateUrl(url);
		return bbs;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String html = responseToURL.getHtml();
					if(html!=null){
						Integer clickNum=0;
						Integer commentNum=0;
						Document doc = Jsoup.parse(html);
						String clickComentstr=doc.select("div#content div.content01 div.artfooter").text();
						if(clickComentstr!=null && clickComentstr.trim().length()!=0){
							if(clickComentstr.indexOf("点击：")!=-1){
								String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("点击：")+"点击：".length());
								if(clickNumstr.indexOf(" ")!=-1){
									clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf(" "));
									try {
										clickNum=Integer.parseInt(clickNumstr);
									} catch (Exception e) {
										// TODO: handle exception
									}
								}
							}
							if(clickComentstr.indexOf("回复：")!=-1){
								String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("回复：")+"回复：".length());
								if(commentNumstr.indexOf(" ")!=-1){
									commentNumstr=commentNumstr.substring(0,commentNumstr.indexOf(" "));
								try {
									commentNum=Integer.parseInt(commentNumstr);
								} catch (Exception e) {
									// TODO: handle exception
								}
								}
							}
						}else{
							 clickComentstr=doc.select("html body div.col form div.colbox div#moretab.ledBox div.top div.date").text();
							 if(clickComentstr!=null && clickComentstr.trim().length()!=0){
								 if(clickComentstr.indexOf("点击：")!=-1){
										String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("点击：")+"点击：".length());
										if(clickNumstr.indexOf(" ")!=-1){
											clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf(" "));
											try {
												clickNum=Integer.parseInt(clickNumstr);
											} catch (Exception e) {
												// TODO: handle exception
											}
										}
									}
									if(clickComentstr.indexOf("回复：")!=-1){
										String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("回复：")+"回复：".length());


										try {
											commentNum=Integer.parseInt(commentNumstr);
										} catch (Exception e) {
											// TODO: handle exception
										}

									}
							 }
						}
						meta.setClickNum(clickNum);
						meta.setCommentNum(commentNum);


						return meta;
					}

				}
			}
		}
		return null;
	}
    
	
}
